package com.pcitc.paas.feign.demo.provider.server.controller;

import com.pcitc.paas.feign.demo.api.ProviderApi;
import com.pcitc.paas.feign.demo.api.model.Person;
import com.pcitc.paas.feign.demo.api.utils.JsonUtils;
import feign.Response;
import lombok.SneakyThrows;
import org.apache.commons.io.IOUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import java.io.File;
import java.io.FileInputStream;

/**
 * description: <br>
 * createTime: 2021/6/21 17:50 <br>
 *
 * @author zzh
 */
@Controller
public class ProviderController extends BaseController implements ProviderApi {

    public ProviderController() {
    }


    @SneakyThrows
    @ResponseBody
    public String testRequestBody(@RequestBody Person person) {
        System.out.println(JsonUtils.toJsonString(person));
        return "OK";
    }


    @ResponseBody
    public String test1(String test, boolean flag, Person person) {
        System.out.println(flag);
        System.out.println(person.getName());
        System.out.println(person.getAge());
        System.out.println(person.getNickName());
        return "Hello world!";
    }


    @ResponseBody
    public String test2(String test, boolean flag, @RequestBody Person person) {
        System.out.println(flag);
        System.out.println(person.getName());
        System.out.println(person.getAge());
        System.out.println(person.getNickName());
        return "Hello world!";
    }


    @ResponseBody
    public String testPathVariable(@PathVariable String test, String name) {
        System.out.println(test);
        return "OK";
    }


    @SneakyThrows
    @ResponseBody
    public String testUpload1(String test, MultipartFile file) {
        System.out.println("===========================");
        System.out.println(new String(file.getBytes()));
        System.out.println("===========================");
        return "OK";
    }


    @SneakyThrows
    @ResponseBody
    public String testUpload2(String test, MultipartFile[] file) {
        System.out.println("===========================");
        MultipartFile[] var3 = file;
        int var4 = file.length;

        for (int var5 = 0; var5 < var4; ++var5) {
            MultipartFile multipartFile = var3[var5];
            System.out.println(new String(multipartFile.getBytes()));
        }
        System.out.println("===========================");
        return "OK";
    }


    @SneakyThrows
    public ResponseEntity<byte[]> testDownload1(String test) {
        File file = new File("d:\\download.txt");

        try (FileInputStream inputStream = new FileInputStream(file);
             ServletOutputStream outputStream = this.response.getOutputStream()) {
            IOUtils.copy(inputStream, outputStream);
        }

        return null;
    }


    @SneakyThrows
    public ResponseEntity<byte[]> testDownload2(String test) {
        File file = new File("d:\\download.txt");
        byte[] b = new byte[0];

        try (FileInputStream inputStream = new FileInputStream(file);) {
            b = new byte[(new FileInputStream(file)).available()];
            inputStream.read(b);
            HttpHeaders heads = new HttpHeaders();
            heads.add("Content-Disposition", "attachment;filename=download.txt");
            heads.add("Content-Type", "application/json");
            return new ResponseEntity(b, heads, HttpStatus.OK);
        }
    }


    @SneakyThrows
    public Response testDownload3(String test) {
        File file = new File("d:\\download.txt");
        this.response.addHeader("Content-Disposition", "attachment;filename=download.txt");

        try (ServletOutputStream outputStream = this.response.getOutputStream();
             FileInputStream inputStream = new FileInputStream(file)) {
            IOUtils.copy(inputStream, outputStream);
        }
        return null;
    }
}